//
//  AppDelegate.m
//  ZDB
//

//

#import "AppDelegate.h"
#import "ZBDTabBarController.h"
#import "CheckVersionAlearView.h"
#import "CoreNewFeatureVC.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
+ (AppDelegate *)shareAppdelegate
{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    
    if (@available(iOS 13,*)) {
        return YES;
    } else
    {
        IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
        manager.enable = YES; // 控制整个功能是否启用。
        manager.shouldResignOnTouchOutside =YES; // 控制点击背景是否收起键盘
        //    manager.shouldToolbarUsesTextFieldTintColor =NO; // 控制键盘上的工具条文字颜色是否用户自定义
        manager.enableAutoToolbar =YES; // 控制是否显示键盘上的工具条
        //    manager.toolbarManageBehaviour =IQAutoToolbarByTag;
        [[IQKeyboardManager sharedManager] setToolbarManageBehaviour:IQAutoToolbarByPosition];
        
        //判断是否需要显示：（内部已经考虑版本及本地版本缓存）
        BOOL canShow = [CoreNewFeatureVC canShowNewFeature];
        /*初始化rootvc*/
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor=WHITECOLOR;
        [self.window makeKeyAndVisible];
        if(canShow){
             NewFeatureModel *m1 = [NewFeatureModel model:[UIImage imageNamed:@"f1.png"]];
             NewFeatureModel *m2 = [NewFeatureModel model:[UIImage imageNamed:@"f2.png"]];
             NewFeatureModel *m3 = [NewFeatureModel model:[UIImage imageNamed:@"f3.png"]];

             if (KIsIPhoneX) {
                 self.window.rootViewController = [CoreNewFeatureVC newFeatureVCWithModels:@[ [NewFeatureModel model:[UIImage imageNamed:@"x1.png"]], [NewFeatureModel model:[UIImage imageNamed:@"x2.png"]], [NewFeatureModel model:[UIImage imageNamed:@"x3.png"]],] enterBlock:^{
                     NSLog(@"进入主页面");
                       [self getVersion];

                    self.window.rootViewController = [[ZBDTabBarController alloc] init];
                 }];
             }else{
                 self.window.rootViewController = [CoreNewFeatureVC newFeatureVCWithModels:@[m1,m2,m3] enterBlock:^{
                     NSLog(@"进入主页面");
                       [self getVersion];

           self.window.rootViewController = [[ZBDTabBarController alloc] init];

                 }];
             }
           

         }else{
              [self getVersion];

             self.window.rootViewController = [[ZBDTabBarController alloc] init];
         }
       //  self.window.rootViewController = [[ZBDTabBarController alloc] init];
        /*适配iOS11*/
        [UITableView appearance].estimatedRowHeight = 10;
        [UITableView appearance].estimatedSectionHeaderHeight = 0;
        [UITableView appearance].estimatedSectionFooterHeight = 0;
        if (@available(iOS 11.0, *)){
            [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
       
        return YES;
    }
   

}
- (void)getVersion
{
    
    WeakSelf
    NSString  * getAppVersionByType = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getAppVersionByType"];
    [[SmileHttpTool sharedInstance] POST:getAppVersionByType parameters:@{@"type":@"2"} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        NSDictionary * dic  = responseObject;
        if (kDictIsEmpty(dic)) {
            return ;
        }
        NSString * content = dic[@"content"];//更新内容
        NSString * isUpdate =dic[@"isUpdate"]; //1为建议升级，2为强制升级
        NSString *  url =  dic[@"url"]; //更新提示
        NSString *  Version = [ZBDUnit getVersion];
        NSString * version_code = dic[@"version"];
       
        //本地版本大于等于线上的，不需要提示更新，如果本地版本小于线上的，就需要提示更新
        if ([[Version stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue] >= [[version_code stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue] )
        {
           weakSelf.isUpdata = NO;
        }else
        {
            weakSelf.isUpdata = YES;
        }
        if ([Version isEqualToString:version_code]) {
            weakSelf.isUpdata = NO;
        }
        if (weakSelf.isUpdata) {
            if (isUpdate.integerValue==1) {
                 [weakSelf VersionupdateWithmessage:content openUrl:url confirm:@"马上升级" cancel:nil state:1];
            }else
            {
                 [weakSelf VersionupdateWithmessage:content openUrl:url confirm:@"马上升级" cancel:@"我再想想" state:2];
            }
            
        }
        
    } failure:^(id responseObject, NSError *error) {
        
    }];
  
}
- (void)VersionupdateWithmessage:(NSString *)message
                         openUrl:(NSString *)url
                         confirm:(NSString *)confirm
                          cancel:(NSString *)cancel
                           state:(NSInteger )state
{
    
    CheckVersionAlearView * aleartVC = [[CheckVersionAlearView alloc] initWithmessage:message  titleStr:@"版本更新" openUrl:url confirm:confirm cancel:cancel state:state RechargeBlock:^{
        
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])
        {
            
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
        }else
        {
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
        }
    }];
    [aleartVC showLXAlertView];
}



#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


@end
